package com.ssm.controller;

import com.ssm.entity.ElcRoleInfotype;
import com.ssm.entity.SysUserRole;
import com.ssm.service.ElcRoleInfotypeService;
import com.ssm.service.SysUserRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (SysUserRole)表控制层
 *
 * @author makejava
 * @since 2021-06-01 10:41:46
 */
@RestController
@RequestMapping("sysUserRole")
public class SysUserRoleController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private ElcRoleInfotypeService elcRoleInfotypeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SysUserRole selectOne(Integer id) {
        return this.sysUserRoleService.queryById(id);
    }

    @GetMapping("query/{id}")
    public Integer queryById(@PathVariable Integer id) {
        Integer roleId =0;
        SysUserRole sysUserRole = sysUserRoleService.queryByUserId(id);
        if (sysUserRole!=null){
            roleId =  sysUserRole.getRoleId();
        }

        return roleId;
    }
    @PostMapping("setPermissionRole/{userId}/{roleId}")
    public String setPermissionRole(@PathVariable Integer userId,@PathVariable Integer roleId){
        SysUserRole sysUserRole = sysUserRoleService.queryByUserId(userId);
        if (sysUserRole != null){
            sysUserRoleService.deleteById(sysUserRole.getId());
        }

        SysUserRole sysUserRole1 = new SysUserRole();
        sysUserRole1.setRoleId(roleId);
        sysUserRole1.setUserId(userId);
        sysUserRole1.setCreateTime(new Date());
        sysUserRole1.setUpdateTime(new Date());
        sysUserRoleService.insert(sysUserRole1);

        List<ElcRoleInfotype> elcRoleInfotypes = elcRoleInfotypeService.queryByUserId(userId);
        if (elcRoleInfotypes != null){
            for (ElcRoleInfotype elcRoleInfotype:elcRoleInfotypes){
                elcRoleInfotypeService.deleteById(elcRoleInfotype.getId());
            }
        }
        return "success";
    }

    @PostMapping("setPermissionInfoType/{userId}/{infoTypeId}")
    public String setPermissionInfoType(@PathVariable Integer userId,@PathVariable Integer[] infoTypeId){
        List<ElcRoleInfotype> elcRoleInfotypes = elcRoleInfotypeService.queryByUserId(userId);
        if (elcRoleInfotypes != null){
            for (ElcRoleInfotype elcRoleInfotype:elcRoleInfotypes){
                elcRoleInfotypeService.deleteById(elcRoleInfotype.getId());
            }
        }
        ElcRoleInfotype elcRoleInfotype = new ElcRoleInfotype();
        elcRoleInfotype.setUserId(userId);
        for (int i=0;i<infoTypeId.length;i++){
            elcRoleInfotype.setInfoTypeId(infoTypeId[i]);
            elcRoleInfotypeService.insert(elcRoleInfotype);
        }
        return "success";
    }

    @PostMapping("setPermission/{userId}/{roleId}/{infoTypeId}")
    public String setPermission(@PathVariable Integer userId,@PathVariable Integer roleId,@PathVariable Integer[] infoTypeId){
        SysUserRole sysUserRole = sysUserRoleService.queryByUserId(userId);
        if (sysUserRole != null){
            sysUserRoleService.deleteById(sysUserRole.getId());
        }

        SysUserRole sysUserRole1 = new SysUserRole();
        sysUserRole1.setRoleId(roleId);
        sysUserRole1.setUserId(userId);
        sysUserRole1.setCreateTime(new Date());
        sysUserRole1.setUpdateTime(new Date());
        sysUserRoleService.insert(sysUserRole1);

        List<ElcRoleInfotype> elcRoleInfotypes = elcRoleInfotypeService.queryByUserId(userId);
        if (elcRoleInfotypes != null){
            for (ElcRoleInfotype elcRoleInfotype:elcRoleInfotypes){
                elcRoleInfotypeService.deleteById(elcRoleInfotype.getId());
            }
        }
        ElcRoleInfotype elcRoleInfotype = new ElcRoleInfotype();
        elcRoleInfotype.setUserId(userId);
        for (int i=0;i<infoTypeId.length;i++){
            elcRoleInfotype.setInfoTypeId(infoTypeId[i]);
            elcRoleInfotypeService.insert(elcRoleInfotype);
        }
        return "success";
    }

}
